    /*  
    championBuilder - Champions Online character planner.
    Copyright (C) 2009 Moritz Hartmeier
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You can find the GNU General Public License in the data folder.
    */


package ch.zarzu.champions.builder.gui;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import ch.zarzu.champions.builder.App;
import ch.zarzu.champions.builder.logic.*;

public class SearchPane extends JLayeredPane implements KeyListener {
    private static SearchPane instance = null;
    
    private SpringLayout layout;
    private Listener listener;
    private JTextField search_field;
    
    private SearchPane() {
        listener = Listener.getInstance();
    
        layout = new SpringLayout();
        setLayout(layout);
        
        draw();
    }
    
    private void draw() {
        search_field = new JTextField();
        search_field.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        search_field.setBackground(App.DARK_BLUE);
        search_field.setFont(new Font("Comic Sans MS", Font.BOLD, 13));
        search_field.setForeground(Color.WHITE);
        search_field.setCaretColor(Color.WHITE);
        search_field.addKeyListener(this);
        search_field.setPreferredSize(new Dimension(200, 20));
        
        add(search_field);

        layout.putConstraint(SpringLayout.WEST, search_field, 20, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.EAST, this, 0, SpringLayout.EAST, search_field);
        layout.putConstraint(SpringLayout.SOUTH, this, 0, SpringLayout.SOUTH, search_field);
    }
    
    public void keyTyped(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER)
            listener.startSearch(search_field.getText());
    }
    public void keyReleased(KeyEvent e) {
        // searches after every change, this is heavy on the machine though
        /*if(e.getKeyCode() != KeyEvent.VK_LEFT && e.getKeyCode() != KeyEvent.VK_RIGHT) {
            listener.startSearch(search_field.getText());
            search_field.requestFocus();
        }*/
    }
    
    public static SearchPane getInstance() {
        if(instance == null)
            instance = new SearchPane();
        return instance;
    }
}
